//
//  WZHNormalPop.swift
//  ShiGuangJiaSwift
//
//  Created by 汪志浩 on 2023/3/9.
//

import Foundation
public class WZHNormalPop: WZHPopView {
    
    @IBOutlet var CTN: UIView!
    @IBOutlet weak var content: UILabel!
    @IBOutlet weak var title: UILabel!
    @IBOutlet weak var cancelBtn: UIButton!
    @IBOutlet weak var queDingBtn: UIButton!
    
    public var complete : (()->())?
    
    public required init?(coder: NSCoder) {
        super.init(coder: coder)
        configUI()
    }
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        configUI()
    }
    
    public func configUI() {
        let view = Bundle.main.loadNibNamed("WZHNormalPop", owner: self)?.first as! UIView
        self.addSubview(view)
        view.snp.makeConstraints { make in
            make.left.right.top.equalTo(0)
        }
        
        ///添加渐变色
        queDingBtn.layer.insertSublayer(self.gradientLayer, at: 0)
    }
    
    /// 渐变色：默认从上到下
    public private var gradientLayer: CAGradientLayer = {
        let g = CAGradientLayer()
        g.colors = [UIColor(hex: "#6174FF")!.cgColor,
                    UIColor(hex: "#3D54FC")!.cgColor]
        //改为从左到右 的渐变
        g.startPoint = CGPoint(x: 0, y: 1)
        g.endPoint = CGPoint(x: 1, y: 1)
        return g
    }()
    
    public override func layoutSubviews() {
        super.layoutSubviews()
        self.gradientLayer.frame = queDingBtn.bounds
    }
    
    
    /**显示**/
    public static func show(title:String?,
                     content:String?=nil,
                     quxiaoTitle:String?="取消",
                     quedingTitle:String="确定",
                     complete:(()->())?=nil){
        let view = WZHNormalPop()
        view.title.isHidden = title==nil
        if !view.title.isHidden  {
            view.title.text = title
        }
        
        view.content.isHidden = content==nil
        if !view.content.isHidden {
            view.content.text = content
        }
        
        view.cancelBtn.isHidden = quxiaoTitle==nil
        view.cancelBtn.setTitle(quxiaoTitle, for: .normal)
        
        view.queDingBtn.setTitle(quedingTitle, for: .normal)
        view.complete = complete
        view.show()
    }
    
    public  override func config_size() -> CGSize {
        self.frame = CGRect(x: 0, y: 0, width: 280, height: 500)
        self.layoutIfNeeded()
        return CGSize(width: 280, height: CTN.frame.height)
    }
    
    /**配置pop,子类可重写**/
    public override func config_pop(pop:OverlayController){
        super.config_pop(pop: pop)
        
        pop.isDismissOnMaskTouched = !self.cancelBtn.isHidden
   
    }
    
    @IBAction func cancel(_ sender: Any) {
        self.disMiss()
    }
    
    @IBAction func queding(_ sender: Any) {
        self.disMiss(complete: {[weak self] in
            guard let `self` = self else { return }
            self.complete?()
        })
    }
}
